<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWeProduct
 *
 * @author i062261
 */
class BAppCommon extends BApp {

    //put your code here

    public function run($data = NULL) {
        parent::run($data);
        //$this->addEventType();
    }

    public function addEventType($typeData = NULL) {
        $category = new BACategory();
        $rootNode = $category->getRootNode();
        $typeNode = $rootNode->getChild('eventType');
        //add event type

        if (!$typeNode instanceof BACategory) {
            $typeNode = $rootNode->addChild('eventType');
        }

        if (is_array($typeData)) {
            foreach ($typeData as $type) {
                $typeNode->addChild($type);
            }
        }

        if (is_string($typeData)) {
            $typeNode->addChild($typeData);
        }
    }

    /**
     * 
     * @param type $data, if data = 'admin', will return all event for admin operation
     * @return type
     */
    public function getEventList($data = NULL) {

        if ($data == NULL) {
            $data = Blyn::app()->getActiveOperation()->getName();
        }

        $typeList = $this->getEventTypeList($data);

        $list = array();
        foreach ($typeList as $id => $typeName) {
            $type = new BACategory($id);
            $events = $type->getMembers(new BTreeList());
            $list += $events;
        }
        //$eventTable = new BAEventSheet();
        //$eventList = $eventTable->getView();

        return $list;
    }

    /**
     * 
     * @param type $data, null or string, if null, return all types, if string
     * @return type
     */
    public function getEventTypeList($data = NULL) {
        if ($data == NULL) {
            //$data = Blyn::app()->getActiveOperation()->getName();
        }
        $category = new BACategory();
        //$category->setOwner(Blyn::app()->getActiveApp());
        $parentEventType = 'eventType';
        if (is_string($data) && $data != NULL) {
            $parentEventType = 'eventType.' . $data;
        }
        $eventTypeNode = $category->getRootNode()->getChild($parentEventType);
        if ($eventTypeNode) {
            $eventTypeFullName = $eventTypeNode->getFullName();
            $children = $eventTypeNode->getChildren();
            $list = array();
            foreach ($children as $id => $typeNode) {
                $fullName = $typeNode->getFullName();
                $name = substr($fullName, strlen($eventTypeFullName) + 1);
                $list[$id] = $name;
            }
            return $list;
        }
        return FALSE;
    }

    public function getEventTypeName($event) {
        $category = new BACategory();
        $eventTypeNode = $category->getRootNode()->getChild('eventType');
        $eventTypeFullName = $eventTypeNode->getFullName();
        if ($event != NULL) {
            $eventType = BACategory::getMemberCategory($event);
            $typeList = $this->getEventTypeList();
            $eventTypeId = $eventType->getId();
            return $typeList[$eventTypeId];
        }
    }

    public function getEventType($event) {
        if ($event != NULL) {
            $eventType = BACategory::getMemberCategory($event);
            return $eventType;
        }
    }

    public function getDataTypeList($data = NULL) {
        $typeList = array(
            'string' => 'String',
            'text' => 'Text',
            'number' => 'Number',
            'datetime' => 'DateTime',
            'image' => 'Image',
            'dropdown' => 'dropDown'
        );
        return $typeList;
    }

    public function showActiveFieldByDataType($dataType = 'string', $modelData = array()) {

        if ($dataType == NULL) {
            $dataType = 'string';
        }

        $model = NULL;
        $form = NULL;
        $field = NULL;
        $data = NULL;
        $htmlOptions = array();

        if (isset($modelData['model']))
            $model = $modelData['model'];

        if (isset($modelData['field']))
            $field = $modelData['field'];

        if (isset($modelData['data']))
            $data = $modelData['data'];

        if (isset($modelData['htmlOptions']))
            $htmlOptions = $modelData['htmlOptions'];

        $dataType = strtolower($dataType);

        if (strtolower($dataType) == 'string' || strtolower($dataType) == 'number') {
            if ($form != NULL) {
                return $form->activeTextField($model, $field, $htmlOptions);
            } else {
                return CHtml::activeTextField($model, $field, $htmlOptions);
            }
        }

        if ($dataType == 'text') {
            if ($form != NULL) {
                return $form->activeTextArea($model, $field, $htmlOptions);
            } else {
                return CHtml::activeTextArea($model, $field, $htmlOptions);
            }
        }

        if ($dataType == 'dropdown') {
            if ($data == NULL)
                $data = array();
            if ($form != NULL) {
                return $form->activeDropDownList($model, $field, $data, $htmlOptions);
            } else {
                return CHtml::activeDropDownList($model, $field, $data, $htmlOptions);
            }
        }

        if ($dataType == 'datetime') {
            $htmlOptions = array('data-format' => "yyyy-MM-dd hh:mm:ss", 'type' => "text", 'placeholder' => '时间...');
            $str = '';
            $str .='<div class="input-append" id="datetimepicker1">';
            if ($data == NULL)
                $data = array();
            if ($form != NULL) {
                $str .=$form->activeTextField($model, $field, $htmlOptions);
                $str .='<span class="add-on">
                            <i data-date-icon="icon-calendar" data-time-icon="icon-time" class="icon-calendar">
                            </i>
                        </span>
                    </div> ';
                return $str;
            } else {
                $str .=CHtml::activeTextField($model, $field, $htmlOptions);
                $str .='<span class="add-on">
                            <i data-date-icon="icon-calendar" data-time-icon="icon-time" class="icon-calendar">
                            </i>
                        </span>
                    </div> ';
                return $str;
            }
        }

        if ($dataType == 'image') {
            $oName = explode("_", $field);
            $field = 'media_image_' . $oName[2] . '_' . $oName[3];
//            $field = 'image_media_image_attribute';
            $str = '';
            //$str = '<div class="row-fluid"><div class="span6"><nobr style="color: red;font-size:20px">*</nobr>' . 'image' . '</div>';
//            $str .= '<div class="span6">' . CHtml::activeFileField($model, $field);
            $str .= '<div>' . CHtml::activeFileField($model, $field);
            if ($model->$field != NULL) {
                $imageUrl = Yii::app()->request->baseUrl . $model->$field;
                $str.= "<img src=" . $imageUrl . " width=50 height=50 />";
            }

            //$str .= '</div></div>';
            $str .='</div>';

            return $str;
        }
    }

    public function getModelDataByDataType($dataTypeItem) {

        if ($dataTypeItem instanceof BTreeItem) {
            $type = $dataTypeItem->getValue();
            $dataTypeTable = new BASheet('dataTypeData');

            if ($type == 'array') {
                $list = array();
                $dataTypeView = $dataTypeTable->getViewByOwner($dataTypeItem);
                foreach ($dataTypeView as $list) {
                    $keyItem = $list->getItem('key');
                    if ($keyItem instanceof BTreeItem)
                        $key = $keyItem->getValue();

                    $valueItem = $list->getItem('value');
                    if ($valueItem instanceof BTreeItem)
                        $value = $valueItem->getValue();

                    $list[$key] = $value;
                    return $list;
                }
            }
        }
    }
    public function addOperationMenuToSpace($menuData, $operation = NULL) {
        if ($operation == NULL)
            $operation = Blyn::app()->getActiveOperation();
        if (is_string($operation)) {
            $oName = $operation;
            $operation = Blyn::app()->getActiveApp()->getOperation($oName);
        }

        if ($operation instanceof BOperation) {
            $appId = $operation->getApp()->getId();
            $spaceId = Blyn::app()->getActiveSpace()->getId();
//            $spMenu = new BUIMenu(Blyn::app()->getActiveSpace());
            $appMenu = new BUIMenu(Blyn::app()->getActiveApp());
//            $appNode = $spMenu->addChild(array('name' => 'app_' . $appId, 'appId' => $appId));
            $opNode = $appMenu->addChild(array('name' => 'op_' . $operation->getId(),'spaceId'=>$spaceId, 'appId' => $appId));
            if (is_array($menuData)) {
                foreach ($menuData as $name => $value) {
                    $opNode->addChild(array('name' => $name, 'value' => $value));
                }
            }
        }
    }

}
